﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class KHXX_KHXXAdd : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("14") && !ProjectPrivate.GetQX("31"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      if (!ProjectPrivate.GetQX("14"))
      {
        txtKHMC.Enabled = false;
        txtKH.Enabled = false;
        cblYQXZ.Enabled = false;
        ddlKHJB.Enabled = false;
        ddlIC.Enabled = false;
        ddlGLDW.Enabled = false;
      }
      else if (!ProjectPrivate.GetQX("31"))
      {
        txtXXDZ.Enabled = false;
        txtFZR.Enabled = false;
        txtDH.Enabled = false;
        txtSSGX.Enabled = false;
        txtBZ.Enabled = false;
        //txtBZ1.Enabled = false;
        txtBZ2.Enabled = false;
        txtBZ3.Enabled = false;
        //txtBZ4.Enabled = false;
        //txtBZ5.Enabled = false;
      }
      BindYQXZ(cblYQXZ, CommonUse.GetID());
      DataSet ds = KHJB.GetDS_W();
      CommonUse.BindDDL(ds, "khjbid", "khjb", "0", ddlKHJB);
      ds = DWGL.GetDS_W();
      CommonUse.BindDDL(ds, "dwid", "dwmc", "0", ddlGLDW);
      string sID = CommonUse.GetID();
      if (!string.IsNullOrEmpty(sID))
      {
        InitData(sID);
        ASPxHyperLink1.Visible = false;
      }
    }
  }

  public void BindYQXZ(CheckBoxList ddl, string sID)
  {
    ddl.Items.Clear();
    ListItem li = null;
    SqlDataReader dr = YQXZ.GetYHYQXZ(sID);
    while (dr.Read())
    {
      li = new ListItem();
      li.Text = dr["yqxz"].ToString();
      li.Value = dr["yqxzid"].ToString();
      li.Selected = Convert.ToBoolean(dr["qx"].ToString());
      ddl.Items.Add(li);
      if (li.Selected)
        hlYQXZ.Value += li.Text + ",";
    }
    dr.Close();
  }

  protected void InitData(string sID)
  {
    SqlDataReader dr = KHXX.GetXX(sID);
    if (dr.Read())
    {
      txtKHMC.Text = dr["khmc"].ToString();
      hlName.Value = txtKHMC.Text;
      ddlKHJB.SelectedValue = dr["khjbid"].ToString();
      hlKHJB.Value = ddlKHJB.SelectedValue;
      ddlGLDW.SelectedValue = dr["dwid"].ToString();
      hlGLDW.Value = ddlGLDW.SelectedValue;
      ddlIC.SelectedValue = dr["ic"].ToString();
      hlIC.Value = ddlIC.SelectedValue;
      txtKH.Text = dr["kh"].ToString();
      hlKH.Value = txtKH.Text;
      txtXXDZ.Text = dr["xxdz"].ToString();
      hlXXDZ.Value = txtXXDZ.Text;
      txtDH.Text = dr["lxdh"].ToString();
      hlDH.Value = txtDH.Text;
      txtSSGX.Text = dr["khxx_sfsb"].ToString();
      hlSSGX.Value = txtSSGX.Text;
      txtBZ.Text = dr["khxx_bz"].ToString();
      hlBZ.Value = txtBZ.Text;
      //txtBZ1.Text = dr["bz1"].ToString();
      //hlBZ1.Value = txtBZ1.Text;
      txtBZ2.Text = dr["bz2"].ToString();
      hlBZ2.Value = txtBZ2.Text;
      txtBZ3.Text = dr["bz3"].ToString();
      hlBZ3.Value = txtBZ3.Text;
      //txtBZ4.Text = dr["bz4"].ToString();
      //hlBZ4.Value = txtBZ4.Text;
      //txtBZ5.Text = dr["bz5"].ToString();
      //hlBZ5.Value = txtBZ5.Text;
      txtFZR.Text = dr["lxr"].ToString();
      hlFZR.Value = txtFZR.Text;
    }
    dr.Close();
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    if (string.IsNullOrEmpty(cblYQXZ.SelectedValue))
    {
      lbMessage.Text = "请选择用气性质";
      return;
    }
    if (ddlKHJB.SelectedValue == "0")
    {
      lbMessage.Text = "请选择客户级别";
      return;
    }
    if (ddlIC.SelectedValue == "0")
    {
      lbMessage.Text = "请选择ic卡户";
      return;
    }
    if (ddlGLDW.SelectedValue == "0")
    {
      lbMessage.Text = "请选择管理单位";
      return;
    }
    if (string.IsNullOrEmpty(CommonUse.GetID()))
      Insert();
    else
      Update();
  }

  //插入
  protected void Insert()
  {
    //if (ExistID())
    //{
    //  lbMessage.Text = "此日期已存在价格";
    //  return;
    //}
    if (KHXX.ExistMC(txtKHMC.Text, txtKH.Text))
    {
      lbMessage.Text = "客户名称与标识已存在，请重新录入";
      return;
    }
    string sID = CommonUse.GetID("jcxx_khxx", "khid");
    Save(sID);
  }

  protected void Save(string sID)
  {
    int iXZ = 0;
    string yqxz = "";
    for (int i = 0; i < cblYQXZ.Items.Count; i++)
    {
      if (cblYQXZ.Items[i].Selected)
      {
        iXZ++;
        yqxz += cblYQXZ.Items[i].Text + ",";
      }
    }
    //if (ddlIC.SelectedValue == "是" && iXZ > 1)
    //{
    //  lbMessage.Text = "IC卡户只能选择一种用气性质";
    //  return;
    //}
    List<CommandInfo> cis = new List<CommandInfo>();

    cis.Add(CommonUse.DeleteCI("jcxx_khxx", "khid", sID));

    ArrayList arrValue = new ArrayList();
    arrValue.Add(sID);
    arrValue.Add(ddlKHJB.SelectedValue);
    arrValue.Add(ddlGLDW.SelectedValue);
    arrValue.Add(txtKHMC.Text);
    arrValue.Add(ddlIC.SelectedValue);
    arrValue.Add(txtXXDZ.Text);
    arrValue.Add(txtDH.Text);
    arrValue.Add(txtSSGX.Text);
    arrValue.Add(txtBZ.Text);
    arrValue.Add(txtKH.Text);
    arrValue.Add(KHXX.GetYE(sID));
    arrValue.Add(KHXX.GetRQ(sID));
    arrValue.Add(txtFZR.Text);
    arrValue.Add("");
    arrValue.Add(txtBZ2.Text);
    arrValue.Add(txtBZ3.Text);
    arrValue.Add("");
    arrValue.Add(txtBZ2.Text);
    ArrayList arrLengh = new ArrayList();
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("50");
    arrLengh.Add("20");
    arrLengh.Add("200");
    arrLengh.Add("20");
    arrLengh.Add("200");
    arrLengh.Add("200");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("50");
    arrLengh.Add("200");
    arrLengh.Add("200");
    arrLengh.Add("200");
    arrLengh.Add("200");
    arrLengh.Add("200");
    cis.Add(CommonUse.InsertCI("jcxx_khxx", arrValue, arrLengh));
    #region
    cis.Add(CommonUse.DeleteCI("jcxx_khyqxz", "khid", sID));

    for (int i = 0; i < cblYQXZ.Items.Count; i++)
    {
      if (cblYQXZ.Items[i].Selected)
      {
        arrValue = new ArrayList();
        arrValue.Add(cblYQXZ.Items[i].Value);
        arrValue.Add(sID);
        arrLengh = new ArrayList();
        arrLengh.Add("20");
        arrLengh.Add("20");
        cis.Add(CommonUse.InsertCI("jcxx_khyqxz", arrValue, arrLengh));
      }
    }
    #endregion
    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = "编号:" + sID + "  " + txtKHMC.Text + "保存成功";
      if (CommonUse.GetID() != "")
      {
        string nr = "";
        if (txtKHMC.Text != hlName.Value)
          nr += "名称由" + hlName.Value + "改为" + txtKHMC.Text + "  ";
        if (txtKH.Text != hlKH.Value)
          nr += "标识由" + hlKH.Value + "改为" + txtKH.Text + "  ";
        if (yqxz != hlYQXZ.Value)
          nr += "用气性质由" + hlYQXZ.Value + "改为" + yqxz + "  ";
        if (ddlKHJB.SelectedValue != hlKHJB.Value)
          nr += "客户级别由" + ddlKHJB.Items.FindByValue(hlKHJB.Value).Text + "改为" + ddlKHJB.SelectedItem.Text + "  ";
        if (ddlIC.SelectedValue != hlIC.Value)
          nr += "IC卡户由" + ddlIC.Items.FindByValue(hlIC.Value).Text + "改为" + ddlIC.SelectedItem.Text + "  ";
        if (ddlGLDW.SelectedValue != hlGLDW.Value)
          nr += "管理单位由" + ddlGLDW.Items.FindByValue(hlGLDW.Value).Text + "改为" + ddlGLDW.SelectedItem.Text + "  ";
        if (txtXXDZ.Text != hlXXDZ.Value)
          nr += "详细地址由" + hlXXDZ.Value + "改为" + txtXXDZ.Text + "  ";
        if (txtFZR.Text != hlFZR.Value)
          nr += "负责人由" + hlFZR.Value + "改为" + txtFZR.Text + "  ";
        if (txtDH.Text != hlDH.Value)
          nr += "电话由" + hlDH.Value + "改为" + txtDH.Text + "  ";
        //if (txtSSGX.Text != hlSSGX.Value)
        //  nr += "所属管线由" + hlSSGX.Value + "改为" + txtSSGX.Text + "  ";
        if (txtBZ.Text != hlBZ.Value)
          nr += "备注由" + hlBZ.Value + "改为" + txtBZ.Text + "  ";
        //if (txtBZ1.Text != hlBZ1.Value)
        //  nr += "备注1由" + hlBZ1.Value + "改为" + txtBZ1.Text + "  ";
        if (txtBZ2.Text != hlBZ2.Value)
          nr += "备注2由" + hlBZ2.Value + "改为" + txtBZ2.Text + "  ";
        if (txtBZ3.Text != hlBZ3.Value)
          nr += "备注3由" + hlBZ3.Value + "改为" + txtBZ3.Text + "  ";
        //if (txtBZ4.Text != hlBZ4.Value)
        //  nr += "备注4由" + hlBZ4.Value + "改为" + txtBZ4.Text + "  ";
        //if (txtBZ5.Text != hlBZ5.Value)
        //  nr += "备注5由" + hlBZ5.Value + "改为" + txtBZ5.Text + "  ";
        if (nr != "")
          rzcz.InsertRZ("修改客户:客户编码为:" + sID + nr);
        BindYQXZ(cblYQXZ, CommonUse.GetID());
        InitData(sID);
      }
    }

  }

  //修改
  protected void Update()
  {
    Save(CommonUse.GetID());
  }
}
